package com.iplant.ams.serviceImpl.dao.dms;

import com.iplant.ams.service.basicenum.dms.scada.DMSDeviceStatusEnum;
import com.iplant.ams.service.po.dms.DMSDeviceLedger;
import com.iplant.ams.service.po.dms.scada.status.DMSDeviceStatus;
import com.iplant.base.po.OutResult;
import com.iplant.base.utils.annotation.dao.DBBaseDAO;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@DBBaseDAO(DMSDeviceLedger.class)
public class DMSDeviceLedgerDAO extends BaseDAO {

    private static Logger logger = LoggerFactory.getLogger(DMSDeviceLedgerDAO.class);


    private static DMSDeviceLedgerDAO Instance = null;

    private DMSDeviceLedgerDAO() {
        super("");
        this.Init();
    }

    public static DMSDeviceLedgerDAO getInstance() {
        if (Instance == null)
            Instance = new DMSDeviceLedgerDAO();
        return Instance;
    }

    public DMSDeviceLedger DMS_SelectDeviceLedger(BMSEmployee wLoginUser, int wDeviceID, String wDeviceNo,
                                                  String wAssetNo, OutResult<Integer> wErrorCode) {
        Map<String, Object> wParams = new HashMap<>();
        wParams.put("ID", wDeviceID);
        wParams.put("CodeEQ", wDeviceNo);
        wParams.put("AssetNo", wAssetNo);
        return this.SelectPO(wLoginUser, wParams, DMSDeviceLedger.class, wErrorCode);
    }

    public List<DMSDeviceLedger> DMS_SelectDeviceLedgerList(BMSEmployee wLoginUser, List<Integer> wDeviceIDList,
                                                            String wDeviceNo, String wDeviceName,
                                                            List<String> wAssetNoList, int wDeviceType, int wModelID,
                                                            int wFactoryID, int wWorkShopID, int wAreaID,
                                                            int wStatusShow, OutResult<Integer> wErrorCode) {
        Map<String, Object> wParams = new HashMap<>();
        wParams.put("ID", wDeviceIDList);
        wParams.put("CodeEQ", wDeviceNo);
        wParams.put("Name", wDeviceName);
        wParams.put("AssetNo", wAssetNoList);
        wParams.put("DeviceTypeID", wDeviceType);
        wParams.put("ModelID", wModelID);
        wParams.put("FactoryID", wFactoryID);
        wParams.put("WorkShopID", wWorkShopID);
        wParams.put("WorkAreaID", wAreaID);
        wParams.put("StatusShowEnable", wStatusShow);
        return this.SelectPOList(wLoginUser, wParams, DMSDeviceLedger.class, wErrorCode);
    }

    public String DMS_DeviceStatusText(BMSEmployee wLoginUser, int deviceStatus, OutResult<Integer> wErrorCode) {
        String wResult = DMSDeviceStatusEnum.TurnOff.getLabel();
        try {
            if ((deviceStatus & 8) > 0) {
                wResult = "急停";
            } else if ((deviceStatus & 16) > 0) {
                wResult = "报警";
            } else if ((deviceStatus & 128) > 0) {
                wResult = "调试";
            } else if ((deviceStatus & 4) > 0) {
                wResult = "停止";
            } else if ((deviceStatus & 32) > 0) {
                wResult = "手动";
            } else if ((deviceStatus & 64) > 0) {
                wResult = "自动";
            } else if ((deviceStatus & 2) > 0) {
                wResult = "运行";
            } else if ((deviceStatus & 1) > 0) {
                wResult = "开机";
            } else if ((deviceStatus & 1) == 0) {
                wResult = "关机";
            }
        } catch (Exception ex) {
            logger.error("DMS_DeviceStatusText", ex);
        }
        return wResult;
    }
}
